package executors.demo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Demo01 {
  public static void main(String[] args) {
    // 如果固定线程是1的话，即使同时有多个任务需要执行，也会只执行一个
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    
    executorService.execute(() -> {
      try {
        TimeUnit.SECONDS.sleep(5);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }
      System.out.println("线程01");
      System.out.println(Thread.currentThread().getName());
    });
    
    executorService.execute(() -> {
      try {
        TimeUnit.SECONDS.sleep(2);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }
      System.out.println("线程02");
      System.out.println(Thread.currentThread().getName());
    });
  }
}
